/************** init.html **************/
function query_invSaleInfo(page) {
	sys_loading();
	var parm = $('#queryinvSaleInfoForm').serialize() + (page != null ? ('&page=' + page) : '');
	var url = create_url('invSaleInfo-query');
	sys_record_parm('invSaleInfo',parm);
	$.ajax({type:'POST', url:url, data:parm,
		success: function(result) {
			sys_loading_close();
			$('#ResultList').html(result);
		}
	});
}

function shield_invSaleInfo(code,name){
	$.Zebra_Dialog('确定要屏蔽吗？',{
	'type': 'question','title': '警告' ,'buttons':[
       {caption:'屏蔽',callback:function() {
    	   var url = create_url('invSaleInfo-shield');
    	   aidExecute_invSaleInfo(code, url);
		}},
       {caption:'取消',callback:function(){}
       }]
	});
}

function unshield_invSaleInfo(code, name) {
	$.Zebra_Dialog('确定要解除屏蔽吗？',{
	'type': 'question','title': '警告' ,'buttons':[
       {caption:'解除',callback:function() {
    	   var url = create_url('invSaleInfo-unshield');
    	   aidExecute_invSaleInfo(code, url);
		}},
       {caption:'取消',callback:function(){}
       }]
	});
}
function aidExecute_invSaleInfo(code, url) {
	var parm = {'code':code};
	sys_submiting();
	$.ajax({type:'POST', url:url, data:parm,
		success: function(result) {
			sys_submiting_close();
			if ('success' == result) {
				query_invSaleInfo();
			} else {
				sys_msg('操作失败，请刷新页面再次尝试，如果问题依然存在，请联系管理员!');
			}
		}
	});
}
//显示详情
function showSerial_invSaleInfo(code) {
	var url = create_url('invSaleInfo-detail');
	var parm = {'code':code};
	$.colorbox({overlayClose:false, href:url, data:parm, close:'[关闭]', width:'800px', height:'800px',
		open:true,onClosed:function(){
		}
	});
}
//查询商品
function aidQueryCn_invSaleInfo(page) {
	sys_loading();
	var parm = $('#aidQueryCnForm').serialize() + (page != null ? ('&page=' + page) : '');
	var url = create_url('invSaleInfo-sList');
	$.ajax({type:'POST', url:url, data:parm,success: function(result) {
			sys_loading_close();
			$('#ResultList').html(result);
			}
		});
	}

function selectCn_invSaleInfo(i) {
	var a = $('#dc').val();
	if (a == null || a == '' || undefined == a) {
		sys_msg('请先选择出仓库！ ');
		return false;
	} 
	var parm = {'index':i ,'dc' :a};
	var url = create_url('invSaleInfo-sInit');
	$.colorbox({overlayClose:false, href:url, data:parm, close:'[关闭]', width:'900px', height:'700px', open:true, 
			onClosed:function(){
			}
		});
	}

//选择
function chooseCn_invSaleInfo(cc, cname,snc,count,price) {
	 var p = $('#aid_g_p').val();
		$('#cc_' + p).val(cc);
		$('#cname_' + p).val(cname);
		$('#snc_' + p).val(snc);
		$('#c_' + p).html(count);
		$('#p_' + p).val(price);
		if (snc == 1 || snc == ''){
			$('#count_' + p).removeAttr('readonly','readonly');
			$('#biao_' + p).css('display','none');
			$('#count_' + p).val('');
			$('#s_' + p).html("无需序列号");
			$('#div_biao_' + p).empty();//清空
			/*$('#div_biao_ '+ p).html('');*/
		} else if(snc == 2){
			var url = create_url('invReturnRetail-check');
			var parm = {'snc' : snc,'cc' : cc};
			$.ajax({type:'POST', url:url, data:parm,
				success : function(serial) {
					if (serial == '' || serial == 'null' || undefined == serial) {
						$('#count_' + p).removeAttr('onMouseUp', '');
						$('#count_' + p).removeAttr('readonly','readonly');
						$('#s_' + p).html("同类序列号");
						$('#biao_' + p).show();
					} else {
						$('#count_' + p).removeAttr('onMouseUp', '');
						$('#count_' + p).removeAttr('readonly','readonly');
						$('#s_' + p).html("同类序列号,序列号为：" + serial);
						$('#biao_' + p).css('display','none');
					}
				}
			});
		} else if(snc == 3){
			$('#count_' + p).removeAttr('onchange', '');
			$('#s_' + p).html("唯一序列号");
			$('#count_' + p).attr('readonly','readonly');
			$('#biao_' + p).show();
		}
		$.fn.colorbox.close();
}

//查询用户
function aidQueryUser_invSaleInfo(page) {
	sys_loading();
	var parm = $('#queryUserForm').serialize() + (page != null ? ('&page=' + page) : '');
	var url = create_url('invSaleInfo-aidUserList');
	$.ajax({type:'POST', url:url, data:parm,success: function(result) {
		sys_loading_close();
		$('#aidResultList').html(result);
		}
	});
}

function selectUser_invSaleInfo(btn) {
	var p = btn.attr('pib');
	var parm = {'pib':p};
	console.log(parm);
	var url = create_url('invSaleInfo-aidUserInit');
	$.colorbox({overlayClose:false, href:url, data:parm, close:'[关闭]', width:'900px', height:'700px', open:true, 
			onClosed:function(){
			}
		});
	}

//选择用户员
function chooseUser_invSaleInfo(code, realName) {
	var p = $('#aid_g_p').val();
	if (p != '' && p == 1) {
		$('#smc').val(code);
		$('#sname').val(realName);
	}else if(p != '' && p == 2) {
		$('#cac').val(code);
		$('#cashier').val(realName);
	}
	$.fn.colorbox.close();
}

//显示序列号行
function show_invSaleInfo(i){
	$('#biao_' + i).show();
}
//删除多余的空白行
function delete_invSaleInfo(btn,i) {
	var p = btn.parents('.rep');
	if (!p.next().hasClass('rep')) {
		p.next().remove();
	}
	p.remove();
	num_invSaleInfo(i);
}
//添加空白行
function add_invSaleInfo() {
	var tempTbble = $('#tempTable');
	var tr = tempTbble.find('.rep').clone(true);
	var	tr1 = tempTbble.find('.rep').next().clone(true);
	var i = $('#tempTable').attr('i');
	tr.find('.cname').attr({'id':'cname_' + i, 'name':'itemList[' + i + '].cname'}).end()
		.find('.btn-primary').attr({'pib': i,'onclick':'selectCn_invSaleInfo(' + i +')', 'style' :''}).end()
		.find('.cc').attr({'id': 'cc_' + i, 'name': 'itemList[' + i + '].cc'}).end()
		.find('.snc').attr({'id':'snc_' + i, 'name':'itemList[' + i + '].snc'}).end()
		.find('.s').attr('id', 's_' + i).end()
		.find('.c').attr('id', 'c_' + i).end()
		.find('.count').attr({'id' : 'count_' + i,'name': 'itemList[' + i + '].count'}).end()
		.find('.price').attr({'id' : 'p_' + i, 'name': 'itemList[' + i + '].price'}).end()
		.find('.remark').attr({'id': 'remark_' + i, 'name': 'itemList[' + i + '].remark'}).end()
		.find('.to').attr('id' , 'total_' + i).end();
	if($('#ListBody').find('tr.rep').length > 0){
		$('#ListBody').find('tr.rep').first().before(tr);
	}else{
		$('#ListBody').append(tr);
	}
	$('#tempTable').attr('i' , ++i);
}

//给input 添加样式
function addCss_invSaleInfo(element) {
	$(element).find("input:checkbox, input:radio").not('input.ibtn').uniform();
}
//检查数量输入只能是数字
function checkNum_invSaleInfo(obj) {
	var v = obj.val();
	if (/^[1-9][0-9]*$/.test(v)) {
	} else {
		sys_msg('只能填数字,而且不能以0开头哦！');
		obj.val('');
	}
	obj.css('border-color','');
	countTotalPrice();
}

// 统计标签数量
function num_invSaleInfo(i) {
	var td = $('#biao_' + i); // 获取对应的TD
	var num = 0; // 计数器
	td.find('.t'); // 获取所有的input
	$.each(td.find('.t'), function() {
		var v = this.value;
		if ('' != v && undefined != v) {
			num++;
		}
	});
	$('#count_' + i).val(num);//给cou 赋值
} 

// 保存零售信息
function save_invSaleInfo() {
	//验证输入框是否有未填写的
	var ok = false;
	$('#editForm').find('input.not').each(function(e) {
		if ('' == this.value || undefined == this.value) {
			$(this).css('border-color','red');
			ok = false;
		} else {
			ok = true;
		}
	});
	if (!ok) {
		sys_msg('不能有空值！');
		return;
	}
	var parm = $('#editForm').serialize();
	var url = create_url('invSaleInfo-save');
	sys_submiting();
	$.ajax({type:'POST', url:url, data:parm,success: function(result) {
			if (result == 'success') {
				sys_msg('操作成功！');
				sys_load_handle('invSaleInfo');
			} else if (result == 'error') {
				sys_msg('操作失败！');
			} else if(result == 'exists'){
				sys_msg('对不起！该物品库存为0!');
			} else if(result == 'not_find'){
				sys_msg('对不起！库存不足!');
			}
		}
	});
}







//加载商品
function loadCommodity_invSaleInfo($obj) {
	var a = $('#dc').val();
	if (a == null || a == '' || undefined == a) {
		sys_msg('请先选择出仓库！ ');
		return false;
	} 
	var serial =$obj.val();
	if ('' == serial || undefined == serial) 
		return false;
	// 判断页面是否重复
	 var ok = false;
	$('#ListBody').find('input.serial').each(function(e) {
		var b = $(this).val(); 
		if (serial === b) {
			if ($(this).attr('o') == 3){
				sys_msg('序列号重复');
			} else if ($(this).attr('o') == 2){
				sys_msg('同类序列号请直接填写数量！');
			}
			ok = true;
			return;
		} 
	});

	if (ok) {
		return false;
	} 
	var parm = {'serial':serial,'dc':a};
	var url = create_url('invSaleInfo-verify');
	$.ajax({type:'POST', url:url, data:parm, dataType:'json',
		success: function(result) {
			sys_submiting_close();
			if (null != result) {
				addRow_invSaleInfo(result);
			} else {
				sys_msg('序列号不存在 ');
				$obj.focus();
			}
		}
	});
}

// 添加标签
function addRow_invSaleInfo(jobj) {
	 // 判断页面是否重复
	 var v = jobj.serial; 
	
	// 判断序列号状态 1.没有序列号，2.同类序列号， 3.唯一序列号
	if (jobj.snc == 2) {
		var tb = $('#tempTable');
		var i = parseInt(tb.attr('i'));
		
		var row = $('#tempTable').find('.rep').clone(true);
		var serialRow = $('#tempTable').find('.rep').next().clone(true);
		var serialdiv = $('#tempDiv').find('div.rep').clone(true);
		var j = parseInt(serialRow.attr('j'));
		row.find('.cname').attr({'id':'cname_' + i, 'name':'itemList[' + i + '].cname',"value": jobj.cname}).end()
			.find('.cc').attr({'id': 'cc_' + i, 'name': 'itemList[' + i + '].cc',"value": jobj.cc}).end()
			.find('.snc').attr({'id':'snc_' + i, 'name':'itemList[' + i + '].snc',"value": jobj.snc}).end()
			.find('.s').attr({'id': 's_' + i}).html(jobj.s+v).end()
			.find('.c').attr({'id' : 'c_'+ i}).html(jobj.count).end()
			.find('.count').attr({'i' : i,'id' : 'count_' + i,'name': 'itemList[' + i + '].count','value' : '1'}).end()
			.find('.price').attr({'id' : 'p_' + i,'name': 'itemList[' + i + '].price',"value": jobj.price}).end()
			.find('.remark').attr({'id': 'remark_' + i, 'name': 'itemList[' + i + '].remark'}).end()
			.find('.to').attr('id' , 'total_' + i).end();
		serialdiv.find('input.serial').attr({'i' : i,'id': 't_' + j,'o':jobj.snc, 'name': 'itemList[' + i + '].detailList[' + j + '].serial',"value": v}).end()
				.find('.del-tag').attr({'onclick':'javascript: delete_invSaleInfo($(this),' + j + ');'}).end()
				.find('a.show-serial').html(v);
		row.attr('i', i);
		tb.attr('i', ++i);
		serialRow.attr('j', '1');
		$(serialRow.children()[0]).append(serialdiv);
		$('#ListBody').append(row);
		$('#ListBody').append(serialRow.css({'display':'none'}));
	} else if (jobj.snc == 3) {
		var s = jobj.cc;
		var comm = null;
		$('#ListBody').find('input.cc').each(function(e) {
			var b = $(this).val(); 
			if (b == s) {
				// 找到商品编号相同的input
				comm = $(this);
				return false;	// 跳出each 循环
			}
		});
		 if (null != comm) {
			// 找到同样商品编号的 直接把序列号添加到 序列号的地方
			var tempRep = comm.parents('tr.rep');	// 找对应行的 放序列号的TD
			var serialDiv = $('#tempTable').next().find('div.rep').clone(true);	// 序列号
			var tb = $('#tempTable');
			var i = parseInt(tb.attr('i') - 1);
			var j = tempRep.next().attr("j");
			serialDiv.find('input.serial').attr({'i' : i, 'id': 't_' + j, 'o':jobj.snc,'name': 'itemList[' + i + '].detailList[' + j + '].serial',"value": v}).end()
					.find('.del-tag').attr({'onclick':'javascript: delete_invSaleInfo($(this),' + j + ');'}).end()
					.find('a.show-serial').html(v);
			
			tempRep.next().attr('j', ++j);
			 $(tempRep.next().children()[0]).append(serialDiv);
			 
			var count = parseInt(tempRep.find('input.count').val());
			if ('' != count && undefined != count)
				count++;

			tempRep.find('input.count').val(count);
			 
		 } else {
			// 没有找到有相同的商品编号 新增一行 
			var row = $('#tempTable').find('.rep').clone(true);
			var serialRow = $('#tempTable').find('.rep').next().clone(true);
			var serialdiv = $('#tempDiv').find('div.rep').clone(true);
			
			var tb = $('#tempTable');
			var i = parseInt(tb.attr('i'));
			
			var j = parseInt(serialRow.attr('j'));
			row.find('.cname').attr({'id':'cname_' + i, 'name':'itemList[' + i + '].cname',"value": jobj.cname}).end()
				.find('.cc').attr({'id': 'cc_' + i, 'name': 'itemList[' + i + '].cc',"value": jobj.cc}).end()
				.find('.snc').attr({'id':'snc_' + i, 'name':'itemList[' + i + '].snc',"value": jobj.snc}).end()
				.find('.s').attr({'id': 's_' + i}).html(jobj.s).end()
				.find('.c').attr({'id' : 'c_'+ i}).html(jobj.count).end()
				.find('.count').attr({'i' : i,'id' : 'count_' + i,'name': 'itemList[' + i + '].count', 'value':'1', 'readonly':'readonly'}).end()
				.find('.price').attr({'id' : 'p_' + i,'name': 'itemList[' + i + '].price',"value": jobj.price}).end()
				.find('.remark').attr({'id': 'remark_' + i, 'name': 'itemList[' + i + '].remark'}).end()
				.find('.to').attr('id' , 'total_' + i).end();
				
			serialdiv.find('input.serial').attr({'i' : i,'id': 't_' + j,'o':jobj.snc, 'name': 'itemList[' + i + '].detailList[' + j + '].serial',"value": v}).end()
					.find('.del-tag').attr({'onclick':'javascript: delete_invSaleInfo($(this),' + j + ');'}).end()
					.find('a.show-serial').html(v);
			row.attr('i', i);
			tb.attr('i', ++i);
			
			serialRow.attr('j', '1');
			$(serialRow.children()[0]).append(serialdiv);
			$('#ListBody').append(row);
			$('#ListBody').append(serialRow);
			
		 }
	}
	countTotalPrice();
}
